package org.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;



// 服务注册类 用于向myNacos注册服务
public class RegisterService {

    @Autowired
    private CustomConfig customConfig;

    @Autowired
    private RestTemplate restTemplate;



    @Value("${server.port}")
    private int port;

    @PostConstruct
    public void registerService() {
        //先赋值
        String NACOS_SERVER_URL = "http://" + customConfig.getServerAddr() + "/nacos/register";
        String SERVICE_NAME = customConfig.getServiceName();


        System.out.println("服务注册类启动");
        ServiceInstance instance = new ServiceInstance();
        instance.setServiceName(SERVICE_NAME);
        instance.setIp("127.0.0.1");
        instance.setPort(port);
        System.out.println(port);



        restTemplate.postForObject(NACOS_SERVER_URL, instance, String.class);
    }

}
